package com.ruoyi.system.vo.weishuapay;

import java.util.UUID;
import java.util.logging.Logger;

import com.ruoyi.common.utils.PropertiesConst;
import com.ruoyi.system.vo.CommonUtil;
import com.ruoyi.system.vo.TransUtil;
import com.ruoyi.system.vo.weishuapay.DreBean.PayReturnBean;
import com.ruoyi.system.vo.weishuapay.DreBean.QueryBalanceReturnBean;
import com.ruoyi.system.vo.weishuapay.DreBean.RegisterReturnBean;
import com.ruoyi.system.vo.weishuapay.DreBean.WithDrawReturnBean;

public class DrePayUtil {

	private static Logger logger4J = Logger.getLogger(DrePayUtil.class
			.getSimpleName());

	public static String register(String dsorderid,String idcard,String accountName,
			String mobile,String disorderid) {
		DreBean bean = new DreBean();
		DreBean.RegisterBean trans = bean.newRegisterBean();
		trans.setMethodname(PropertiesConst.WeishuaPayURL_DRE_REGISTER);
		trans.setTranscode(PropertiesConst.WeishuaPay_REGISTER_TRANSCODE_015);
		trans.setVersion(PropertiesConst.WeishuaPay_Version);
		trans.setOrdersn(UUID.randomUUID().toString().replaceAll("-", ""));
		trans.setDsorderid(dsorderid);
		trans.setMerchno(PropertiesConst.WeishuaPay_MERCHNO);
		trans.setAccountName(accountName);
		trans.setIdcard(idcard);
		trans.setMobile(mobile);
		return sendRegister(trans);

	}
	
	public static String register(String dsorderid,String idcard,String accountName,
			String mobile,String bankcard,String username,String province,
			String city,String futureRateValue,String fixAmount) {
		DreBean bean = new DreBean();
		DreBean.RegisterBean trans = bean.newRegisterBean();
		trans.setMethodname(PropertiesConst.WeishuaPayURL_DAEF_REGISTER);
		trans.setTranscode(PropertiesConst.WeishuaPay_REGISTER_TRANSCODE_035);
		trans.setVersion(PropertiesConst.WeishuaPay_Version);
		trans.setOrdersn(UUID.randomUUID().toString().replaceAll("-", ""));
		trans.setDsorderid(dsorderid);
		trans.setMerchno(PropertiesConst.WeishuaPay_MERCHNO);
		trans.setAccountName(accountName);
		trans.setIdcard(idcard);
		trans.setMobile(mobile);
		trans.setBankcard(bankcard);
		trans.setUsername(username);
		trans.setProvince(province);
		trans.setCity(city);
		trans.setFixAmount(fixAmount);
		trans.setFutureRateValue(futureRateValue);
		return sendRegisterF(trans);

	}
	
	
	public static String pay(String dsorderid,String idcard,String accountName,
			String mobile,String subMerchantNo,String amount,String futureRateValue,String bankcard,String cvn2,
			String expireDate,String province,String notifyUrl) {
		DreBean bean = new DreBean();
		DreBean.PayBean trans = bean.newPayBean();
		trans.setMethodname(PropertiesConst.WeishuaPayURL_DRE_PAY);
		trans.setTranscode(PropertiesConst.WeishuaPay_REGISTER_TRANSCODE_015);
		trans.setVersion(PropertiesConst.WeishuaPay_Version);
		trans.setOrdersn(UUID.randomUUID().toString().replaceAll("-", ""));
		trans.setDsorderid(dsorderid);
		trans.setMerchno(PropertiesConst.WeishuaPay_MERCHNO);
		trans.setSubMerchantNo(subMerchantNo);
		trans.setAmount(amount);
		trans.setFutureRateValue(futureRateValue);
		trans.setBankcard(bankcard);
		trans.setFixAmount("0");//消费单比手续费为0
		trans.setIdcard(idcard);
		trans.setAccountName(accountName);
		trans.setMobile(mobile);
		trans.setCvn2(cvn2);
		trans.setExpireDate(expireDate);
		trans.setProvince(province);
		trans.setNotifyUrl(notifyUrl);
		return sendPay(trans);
	}
	
	public static String payF(String dsorderid,String subMerchantNo,String bankcard,String cvn2,String expireDate,
			String province,String notifyUrl,String userIP,String mcc,String productName,String productDesc,String amount) {
		DreBean bean = new DreBean();
		DreBean.PayBean trans = bean.newPayBean();
		trans.setMethodname(PropertiesConst.WeishuaPayURL_DRE_PAY);
		trans.setTranscode(PropertiesConst.WeishuaPay_REGISTER_TRANSCODE_035);
		trans.setVersion(PropertiesConst.WeishuaPay_Version);
		trans.setOrdersn(UUID.randomUUID().toString().replaceAll("-", ""));
		trans.setDsorderid(dsorderid);
		trans.setMerchno(PropertiesConst.WeishuaPay_MERCHNO);
		trans.setSubMerchantNo(subMerchantNo);
		trans.setBankcard(bankcard);
		trans.setAmount(amount);
		trans.setCvn2(cvn2);
		trans.setExpireDate(expireDate);
		trans.setProvince(province);
		trans.setNotifyUrl(notifyUrl);
		trans.setUserIP(userIP);
		trans.setMcc(mcc);
		trans.setProductName(productName);
		trans.setProductDesc(productDesc);
		return sendPayF(trans);
	}
	
	public static String sendSms(String dsorderid, String subMerchantNo, String bankcard, String username,
			String idcard, String mobile, String cvn2, String expireDate) {
		DreBean bean = new DreBean();
		DreBean.PayBean trans = bean.newPayBean();
		trans.setMethodname(PropertiesConst.WeishuaPayURL_DAEF_signSms);
		trans.setTranscode(PropertiesConst.WeishuaPay_REGISTER_TRANSCODE_035);
		trans.setVersion(PropertiesConst.WeishuaPay_Version);
		trans.setOrdersn(UUID.randomUUID().toString().replaceAll("-", ""));
		trans.setDsorderid(dsorderid);
		trans.setMerchno(PropertiesConst.WeishuaPay_MERCHNO);
		trans.setSubMerchantNo(subMerchantNo);
		trans.setBankcard(bankcard);
		trans.setCvn2(cvn2);
		trans.setExpireDate(expireDate);
		trans.setUsername(username);
		trans.setIdcard(idcard);
		trans.setMobile(mobile);
		return sendPayF(trans);
	}
	
	public static String signSms(String dsorderid,String subMerchantNo, String bankcard,
			String orderNo, String smsCode) {
		DreBean bean = new DreBean();
		DreBean.PayBean trans = bean.newPayBean();
		trans.setMethodname(PropertiesConst.WeishuaPayURL_DAEF_signSmsConfirm);
		trans.setTranscode(PropertiesConst.WeishuaPay_REGISTER_TRANSCODE_035);
		trans.setVersion(PropertiesConst.WeishuaPay_Version);
		trans.setOrdersn(UUID.randomUUID().toString().replaceAll("-", ""));
		trans.setDsorderid(dsorderid);
		trans.setMerchno(PropertiesConst.WeishuaPay_MERCHNO);
		trans.setSubMerchantNo(subMerchantNo);
		trans.setBankcard(bankcard);
		trans.setOrderNo(orderNo);
		trans.setSmsCode(smsCode);
		return sendPayF(trans);
	}
	
	public static String queryBalanceF(String dsorderid,String subMerchantNo) {
		DreBean bean = new DreBean();
		DreBean.PayBean trans = bean.newPayBean();
		trans.setMethodname(PropertiesConst.WeishuaPayURL_DAEF_queryBalance);
		trans.setTranscode(PropertiesConst.WeishuaPay_REGISTER_TRANSCODE_035);
		trans.setVersion(PropertiesConst.WeishuaPay_Version);
		trans.setOrdersn(UUID.randomUUID().toString().replaceAll("-", ""));
		trans.setDsorderid(dsorderid);
		trans.setMerchno(PropertiesConst.WeishuaPay_MERCHNO);
		trans.setSubMerchantNo(subMerchantNo);
		return sendPayF(trans);
	}
	
	public static String withDrawF(String dsorderid, String subMerchantNo, String bankcard, String amount,
			String handleType) {
		DreBean bean = new DreBean();
		DreBean.PayBean trans = bean.newPayBean();
		trans.setMethodname(PropertiesConst.WeishuaPayURL_DAEF_withDraw);
		trans.setTranscode(PropertiesConst.WeishuaPay_REGISTER_TRANSCODE_035);
		trans.setVersion(PropertiesConst.WeishuaPay_Version);
		trans.setOrdersn(UUID.randomUUID().toString().replaceAll("-", ""));
		trans.setDsorderid(dsorderid);
		trans.setMerchno(PropertiesConst.WeishuaPay_MERCHNO);
		trans.setSubMerchantNo(subMerchantNo);
		trans.setAmount(amount);
		trans.setHandleType(handleType);
		trans.setBankcard(bankcard);
		return sendPayF(trans);
	}
	
	public static String update(String dsorderid,String futureRateValue,String handleType,String subMerchantNo) {
		DreBean bean = new DreBean();
		DreBean.PayBean trans = bean.newPayBean();
		trans.setMethodname(PropertiesConst.WeishuaPayURL_DAEF_update);
		trans.setTranscode(PropertiesConst.WeishuaPay_REGISTER_TRANSCODE_035);
		trans.setVersion(PropertiesConst.WeishuaPay_Version);
		trans.setOrdersn(UUID.randomUUID().toString().replaceAll("-", ""));
		trans.setDsorderid(dsorderid);
		trans.setMerchno(PropertiesConst.WeishuaPay_MERCHNO);
		trans.setHandleType(handleType);
		trans.setFutureRateValue(futureRateValue);
		trans.setSubMerchantNo(subMerchantNo);
		return sendPayF(trans);
	}
	
	public static String withDraw(String dsorderid,String idcard,String accountName,
			String mobile,String subMerchantNo,String amount,String bankcard,String cvn2,
			String expireDate,String province,String notifyUrl) {
		DreBean bean = new DreBean();
		DreBean.WithDrawBean trans = bean.newWithDrawBean();
		trans.setMethodname(PropertiesConst.WeishuaPayURL_DRE_withDraw);
		trans.setTranscode(PropertiesConst.WeishuaPay_REGISTER_TRANSCODE_015);
		trans.setVersion(PropertiesConst.WeishuaPay_Version);
		trans.setOrdersn(UUID.randomUUID().toString().replaceAll("-", ""));
		trans.setDsorderid(dsorderid);
		trans.setMerchno(PropertiesConst.WeishuaPay_MERCHNO);
		trans.setSubMerchantNo(subMerchantNo);
		trans.setAmount(amount);
		trans.setFixAmount("100");//单比手续费1元，钉死
		trans.setBankcard(bankcard);
		trans.setMobile(mobile);
		trans.setNotifyUrl(notifyUrl);
//		trans.setCardType("DEBIT");储蓄卡
		return sendWithDraw(trans);
	}
	
	public static String queryBalance(String dsorderid,String subMerchantNo) {
		DreBean bean = new DreBean();
		DreBean.QueryBalanceBean trans = bean.newQueryBalanceBean();
		trans.setMethodname(PropertiesConst.WeishuaPayURL_DRE_queryBalance);
		trans.setTranscode(PropertiesConst.WeishuaPay_REGISTER_TRANSCODE_015);
		trans.setVersion(PropertiesConst.WeishuaPay_Version);
		trans.setOrdersn(UUID.randomUUID().toString().replaceAll("-", ""));
		trans.setDsorderid(dsorderid);
		trans.setMerchno(PropertiesConst.WeishuaPay_MERCHNO);
		trans.setSubMerchantNo(subMerchantNo);
		return sendQueryBalance(trans);
	}
	
	
	public static String sendRegister(DreBean.RegisterBean trans) {
		try {
			TransUtil tu = new TransUtil();
			byte[] reponse = tu.packet(trans, PropertiesConst.WeishuaPay_MERCHKEY);
			String response = CommonUtil.post(PropertiesConst.WeishuaPayURL_DRE_REGISTER_URL, reponse);
			logger4J.info("response:"+response);
			return response;
		} catch (Exception e) {
			logger4J.info("err:"+e.getLocalizedMessage());
		}
		return null;
	}
	
	public static String sendRegisterF(DreBean.RegisterBean trans) {
		try {
			TransUtil tu = new TransUtil();
			byte[] reponse = tu.packet(trans, PropertiesConst.WeishuaPay_MERCHKEY);
			String response = CommonUtil.post(PropertiesConst.WeishuaPayURL_DAEF_REGISTER_URL, reponse);
			logger4J.info("response:"+response);
			return response;
		} catch (Exception e) {
			logger4J.info("err:"+e.getLocalizedMessage());
		}
		return null;
	}
	
	public static String sendPay(DreBean.PayBean trans) {
		try {
			TransUtil tu = new TransUtil();
			byte[] reponse = tu.packet(trans, PropertiesConst.WeishuaPay_MERCHKEY);
			String response = CommonUtil.post(PropertiesConst.WeishuaPayURL_DRE_REGISTER_URL, reponse);
			logger4J.info("response:"+response);
			return response;
		} catch (Exception e) {
			logger4J.info("err:"+e.getLocalizedMessage());
		}
		return null;
	}
	
	public static String sendPayF(DreBean.PayBean trans) {
		try {
			TransUtil tu = new TransUtil();
			byte[] reponse = tu.packet(trans, PropertiesConst.WeishuaPay_MERCHKEY);
			String response = CommonUtil.post(PropertiesConst.WeishuaPayURL_DAEF_REGISTER_URL, reponse);
			logger4J.info("response:"+response);
			return response;
		} catch (Exception e) {
			logger4J.info("err:"+e.getLocalizedMessage());
		}
		return null;
	}
	
	public static String sendQueryBalance(DreBean.QueryBalanceBean trans) {
		try {
			TransUtil tu = new TransUtil();
			byte[] reponse = tu.packet(trans, PropertiesConst.WeishuaPay_MERCHKEY);
			String response = CommonUtil.post(PropertiesConst.WeishuaPayURL_DRE_REGISTER_URL, reponse);
			logger4J.info("response:"+response);
			return response;
		} catch (Exception e) {
			logger4J.info("err:"+e.getLocalizedMessage());
		}
		return null;
	}
	
	public static String sendWithDraw(DreBean.WithDrawBean trans) {
		try {
			TransUtil tu = new TransUtil();
			byte[] reponse = tu.packet(trans, PropertiesConst.WeishuaPay_MERCHKEY);
			String response = CommonUtil.post(PropertiesConst.WeishuaPayURL_DRE_REGISTER_URL, reponse);
			logger4J.info("response:"+response);
			return response;
		} catch (Exception e) {
			logger4J.info("err:"+e.getLocalizedMessage());
		}
		return null;
	}
	
	public static boolean isJobSuccess(RegisterReturnBean bean,String response) {
		RegisterReturnBean json2RegisterReturnBean = TransUtil.json2RegisterReturnBean(response);
		if(PropertiesConst.RETURN_CODE_SUCCESS.equals(json2RegisterReturnBean.getReturncode())){
			return true;
		}
		return false;
	}
	
	public static boolean isJobSuccess(PayReturnBean bean,String response) {
		PayReturnBean returnBean = TransUtil.json2PayReturnBean(response);
		if(PropertiesConst.RETURN_CODE_SUCCESS.equals(returnBean.getReturncode())){
			return true;
		}
		return false;
	}
	
	
	public static boolean isJobPaySuccess(PayReturnBean bean,String response) {
		PayReturnBean returnBean = TransUtil.json2PayReturnBean(response);
		if(PropertiesConst.RETURN_CODE_SUCCESS_0003.equals(returnBean.getReturncode())){
			return true;
		}
		return false;
	}
	public static boolean isJobSuccess(WithDrawReturnBean bean,String response) {
		PayReturnBean returnBean = TransUtil.json2PayReturnBean(response);
		if(PropertiesConst.RETURN_CODE_SUCCESS_0003.equals(returnBean.getReturncode())){
			return true;
		}
		return false;
	}
	
	public static boolean isJobSuccess(QueryBalanceReturnBean bean,String response) {
		QueryBalanceReturnBean returnBean = TransUtil.json2queryBalance(response);
		if(PropertiesConst.RETURN_CODE_SUCCESS.equals(returnBean.getReturncode())){
			return true;
		}
		return false;
	}
	
	
	
	public static boolean isJobWithDrawSuccess(WithDrawReturnBean bean,String response) {
		WithDrawReturnBean returnBean = TransUtil.json2withDrawBean(response);
		if(PropertiesConst.RETURN_CODE_SUCCESS_0003.equals(returnBean.getReturncode())){
			return true;
		}
		return false;
	}
	
}
